package mypack;

public class Main {

	/**
	 * @param args
	 */
	
	public static void main(String[] args)
	{
		int iterationNumber=0;
		//Global.gen.createCities2();
		//Global.gen.readFile();
		Global.gen.readTSPLIBFile();
		Global.gen.printMatrix();
		
		//Running of Genetic Algorithm
		long start = System.currentTimeMillis();
		Population population = new Population();
		population.createPopulation();
		population.calculateAllPathLengths();
		population.printPopulation(iterationNumber);

		//while (iterationNumber < Global.MAX_ITERATION_NUMBER && !population.isLast20Same() && !population.isGoodEnough())
		while(iterationNumber < Global.MAX_ITERATION_NUMBER && /*!population.isLast20Same() &&*/ !population.isGoodEnough())
		{
			iterationNumber++;
			System.out.println("iteration number: "+iterationNumber);

			/*
			 * 1-Choose the chromosomes for crossover
			 * 2-Crossover
			 * 3-Mutate
			 * 4-Create the next population
 			 */
			
			for (int i = 0; i < Global.POPULATION_SIZE-1; i=i+2) 
			{
				//int mate1 = population.select();
				//int mate2 = population.select();
				
				int mate1 = population.rankSelect();
				Chromosome c1 = population.currentPopulation[mate1];
				Chromosome c2 = null;
				int mate2 = mate1;
				int counter = 0;
				do {
					mate2 = population.rankSelect();
					//mate2 = population.select();
					c2 = population.currentPopulation[mate2];
					counter++;
				//} while (c1.isSame(c2));
				} while (c1.similarityPercentage(c2) > 70 && counter < Global.POPULATION_SIZE); // benzerlik %80'dan fazla oldugu surece
				
				
				population.currentPopulation[mate1].numberOfChosen++;
				population.currentPopulation[mate2].numberOfChosen++;

				
				Newbies children = population.crossover(mate1, mate2); //crossover
				
				//if (children.child1.mPathLength < population.currentPopulation[mate1].mPathLength)
					population.newPopulation[i] = children.child1.copyChromosome();
				//else
					//population.newPopulation[i] = population.currentPopulation[mate1].copyChromosome();
				
				//if (children.child2.mPathLength < population.currentPopulation[mate2].mPathLength)
					population.newPopulation[i+1] = children.child2.copyChromosome();
				//else
					//population.newPopulation[i+1] = population.currentPopulation[mate2].copyChromosome();
				
			}
			
			population.printPopulation(iterationNumber);
			
			int numOfBestTransfer = 1;//Global.POPULATION_SIZE * 5 / 100;
			population.mutationNewPop();
			population.calculateNewPopPathLengths();
			for (int i = 0; i < numOfBestTransfer; i++) 
			{
				population.newPopulation[Global.POPULATION_SIZE-i-1] = population.currentPopulation[i].copyChromosome();
			}
			
			population.copyNewPopToCurrentPop();
			//population.mutation();
			population.calculateAllPathLengths();
			population.printPopulation(iterationNumber);
			population.printWriter.println("**************----*******");
			
		}
	
		System.out.println("oyun bitti!!");
		population.printWriter.println("oyun bitti!!");
		//population.printWriter.println("the best Fittness : "+theBestFittness);
		population.printWriter.println("the best evaluated pathlength: "+population.theShortestPathLength);
		System.out.println("the best evaluated pathlength: "+population.theShortestPathLength);
		long timeElapsed = System.currentTimeMillis() - start;
		
		float timeInSecond = timeElapsed / (float)1000.0;
		
		System.out.println("Time elapsed in second : "+timeInSecond);
		population.printWriter.println("Time elapsed in second : "+timeInSecond);
		population.printWriter.close();
	}

}
